package com.example.listgame.myfunctions;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.listgame.R;


public class CustomDialog extends Dialog implements View.OnClickListener {

    private TextView mTitle,mMessage,mAgain,mBack;
    private String title,message,again,back;
    private IOnAgainListener againListener;
    private IOnBackListener backListener;

    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public CustomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CustomDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public CustomDialog setAgain(String again,IOnAgainListener listener) {
        this.again = again;
        this.againListener = listener;
        return this;
    }

    public CustomDialog setBack(String back,IOnBackListener listener) {
        this.back = back;
        this.backListener = listener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);

        //设置宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) (size.x * 0.8);   //设置dialog的宽度为屏幕*0.8
        getWindow().setAttributes(p);

        mTitle = findViewById(R.id.tv_title);
        mMessage = findViewById(R.id.tv_message);
        mAgain = findViewById(R.id.tv_again);
        mBack = findViewById(R.id.tv_back);
        if (!TextUtils.isEmpty(title)){
            mTitle.setText(title);
        }
        if (!TextUtils.isEmpty(message)){
            mMessage.setText(message);
        }
        if (!TextUtils.isEmpty(again)){
            mAgain.setText(again);
        }
        if (!TextUtils.isEmpty(back)){
            mBack.setText(back);
        }
        mAgain.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_again:
                if (againListener != null){
                    againListener.onAgain(this);
                }
                dismiss();
                break;
            case R.id.tv_back:
                if (backListener != null){
                    backListener.onBack(this);
                }
                dismiss();
                break;
        }
    }

    public interface IOnAgainListener{
        void onAgain(CustomDialog dialog);
    }

    public interface IOnBackListener{
        void onBack(CustomDialog dialog);
    }
}
